<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务项目" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入服务项目名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商户id" prop="mid">
        <el-input v-model="queryParams.mid" placeholder="请输入商户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="服务类型" prop="typeId" style="width: 220px;">
        <el-select v-model="queryParams.typeId" placeholder="请选择服务类型" clearable>
          <el-option v-for="type in fuwutypeList" :key="type.id" :label="type.type" :value="type.id" />
          <!-- 自定义后缀图标，添加清空按钮 -->
          <template #suffix-icon>
            <!-- 当有选中值时显示清除按钮 -->
            <el-icon v-if="queryParams.typeId" @click="clearTypeId" class="clear-icon">
              <!-- 引入关闭图标 -->
              <el-icon>
                <Close />
              </el-icon>
            </el-icon>
            <!-- 无选中值时显示下拉箭头 -->
            <el-icon v-else>
              <el-icon>
                <ArrowDown />
              </el-icon>
            </el-icon>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['fuwu:fuwudetails:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['fuwu:fuwudetails:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['fuwu:fuwudetails:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['fuwu:fuwudetails:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fuwudetailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="服务项目" align="center" prop="name" />
      <el-table-column label="商户" align="center" prop="mid" >
        <template #default="scope">
          {{ getMerchantName(scope.row.mid) }}
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="typeId">
        <template #default="scope">
          {{ getTypeName(scope.row.typeId) }}
        </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="originalPrice" />
      <el-table-column label="折扣价" align="center" prop="presentPrice" />
      <!-- <el-table-column label="服务介绍" align="center" prop="introduction" /> -->
      <el-table-column label="服务图片" align="center" prop="img" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['fuwu:fuwudetails:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['fuwu:fuwudetails:remove']">删除</el-button>
          <!-- 新增详情按钮 -->
          <el-button link type="info" icon="Document" @click="handleViewDetail(scope.row)"
            v-hasPermi="['fuwu:fuwudetails:view']">服务介绍</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改服务详情对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="fuwudetailsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务项目" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务项目名称" />
        </el-form-item>
        <el-form-item label="商户id" prop="mid">
          <!-- 添加 @blur 事件监听器 -->
          <el-input v-model="form.mid" placeholder="请输入商户id" @blur="handleMerchantIdBlur"  />
        </el-form-item>
        <el-form-item label="服务类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择服务类型">
            <el-option v-for="type in fuwutypecontactList" :key="type.id" :label="getTypeName(type.typeId)" :value="type.typeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="折扣价" prop="presentPrice">
          <el-input v-model="form.presentPrice" placeholder="请输入折扣价" />
        </el-form-item>
        <!-- 修改服务介绍输入框为富文本控件 -->
        <el-form-item label="服务介绍" prop="introduction">
        <editor v-model="form.introduction" :min-height="192"/>
        </el-form-item>
        <el-form-item label="服务图片" prop="img">
          <image-upload v-model="form.img" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
     <!-- 新增查看详情对话框 -->
  <el-dialog :title="`${selectedItem.name} 服务介绍详情`" v-model="detailDialogVisible" width="500px" append-to-body>
    <div v-html="selectedItem.introduction"></div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
  </div>
</template>

<script setup name="Fuwudetails">
import { listFuwudetails, getFuwudetails, delFuwudetails, addFuwudetails, updateFuwudetails } from "@/api/fuwu/fuwudetails"
import { listMerchant} from "@/api/merchant/merchant"
import { listFuwutype } from "@/api/fuwu/fuwutype"
import { listFuwutypecontact} from "@/api/fuwutypecontact/fuwutypecontact"

import { Close, ArrowDown } from '@element-plus/icons-vue';
const { proxy } = getCurrentInstance()

const fuwudetailsList = ref([])
const fuwutypeList = ref([])
const merchantList = ref([])
const fuwutypecontactList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const detailDialogVisible = ref(false)
const selectedItem = ref({})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    mid: null,
    typeId: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查看详情按钮操作 */
function handleViewDetail(row) {
  selectedItem.value = row
  detailDialogVisible.value = true
}
/** 查询商家服务类别关联列表 */
function getList4(mid) {
  loading.value = true
  const tempParams = {
    pageNum: 1,
    pageSize: 10,
    mid: mid
  }
  listFuwutypecontact(tempParams).then(response => {
    fuwutypecontactList.value = response.rows
    loading.value = false
  })
}

/** 查询服务详情列表 */
function getList() {
  loading.value = true
  listFuwudetails(queryParams.value).then(response => {
    fuwudetailsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
/** 查询服务类型列表 */
function getList2() {
  loading.value = true
  listFuwutype(queryParams.value).then(response => {
    fuwutypeList.value = response.rows

  })
}
/** 查询商户列表 */
function getList3() {
  loading.value = true
  listMerchant().then(response => {
    merchantList.value = response.rows
  })
}
//根据服务类型id获取服务类型名称
function getTypeName(typeId) {
  const typeItem = fuwutypeList.value.find(type => type.id === typeId);
  return typeItem ? typeItem.type : '';
}
// 根据商户id获取商户名称
function getMerchantName(merchantId) {
  const merchantItem = merchantList.value.find(merchant => merchant.id === merchantId);
  return merchantItem ? merchantItem.name : '';
}
//根据id获取服务项目名称
function getName(id) {
  const item = fuwudetailsList.value.find(item => item.id === id);
  return item ? item.name : '';
}


// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    mid: null,
    typeId: null,
    originalPrice: null,
    presentPrice: null,
    introduction: null,
    img: null,
    deleted: null
  }
  proxy.resetForm("fuwudetailsRef")
}
/**
 * 清空服务类型选择
 */
function clearTypeId() {
  queryParams.value.typeId = null;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加服务详情"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getFuwudetails(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改服务详情"
    // 触发根据商户 id 获取服务类型
    if (form.value.mid) {
      getList4(form.value.mid);
    }
  })

}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fuwudetailsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateFuwudetails(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addFuwudetails(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除服务详情编号为"' + _ids + '"的数据项？').then(function () {
    return delFuwudetails(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('fuwu/fuwudetails/export', {
    ...queryParams.value
  }, `fuwudetails_${new Date().getTime()}.xlsx`)
}

getList()
getList2()
getList3()

// 新增处理商户 ID 输入框失去焦点的方法
function handleMerchantIdBlur() {
  if (form.value.mid) {
    getList4(form.value.mid);
  }
}

</script>

<style scoped>
.clear-icon {
  cursor: pointer;
}



</style>
